package user;

import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.atlassian.crowd.embedded.api.PasswordCredential;
import com.atlassian.crowd.embedded.api.SearchRestriction;
import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.GroupNotFoundException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.InvalidCredentialException;
import com.atlassian.crowd.exception.InvalidEmailAddressException;
import com.atlassian.crowd.exception.InvalidUserException;
import com.atlassian.crowd.exception.MembershipNotFoundException;
import com.atlassian.crowd.exception.OperationFailedException;
import com.atlassian.crowd.exception.UserNotFoundException;
import com.atlassian.crowd.integration.rest.entity.PasswordEntity;
import com.atlassian.crowd.integration.rest.entity.UserEntity;
import com.atlassian.crowd.integration.rest.service.factory.RestCrowdClientFactory;
import com.atlassian.crowd.model.user.User;
import com.atlassian.crowd.search.EntityDescriptor;
import com.atlassian.crowd.search.builder.QueryBuilder;
import com.atlassian.crowd.search.query.QueryUtils;
import com.atlassian.crowd.search.query.entity.EntityQuery;
import com.atlassian.crowd.search.query.membership.MembershipQuery;
import com.atlassian.crowd.search.util.SearchResultsUtil;
import com.atlassian.crowd.search.query.entity.restriction.MatchMode;
import com.atlassian.crowd.search.query.entity.restriction.TermRestriction;
import com.atlassian.crowd.search.query.entity.restriction.constants.UserTermKeys;
import com.atlassian.crowd.service.client.CrowdClient;

public class AddUser extends JPanel {

	String appLink = "https://code.bestbuy.com/crowd/";  //connection details 
	CrowdClient userConn = new RestCrowdClientFactory().newInstance(appLink,"profile-mgt","1nitialPa55"); 
	RetrieveUserInfo uzInfo = new RetrieveUserInfo();
	RetrieveGroupInfo grpInfo = new RetrieveGroupInfo();


	int maxResults = 0;
	int startIndex = EntityQuery.MAX_MAX_RESULTS;


	/*
	 * addd use to crowd
	 * confirm user does not exist on crowd , then add user after that request user password reset
	 */
	public User creatUser(String userName, String fName, String lName, String email, String dName, PasswordEntity pWord, boolean status) throws InvalidUserException, InvalidCredentialException, OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException, UserNotFoundException, HeadlessException, InvalidEmailAddressException{
		UserEntity uz = new UserEntity (userName, fName,lName,dName,email,pWord, status);
		String j = "123456"; 

		PasswordCredential k = new PasswordCredential (j,true ); 
		if (checkIfUserExist(uz.getName())!=true){
			userConn.addUser(uz,k);
			resetPassword(uz.getName());
		} else{
			JOptionPane.showMessageDialog(null, "User already exist", "User already exist", JOptionPane.INFORMATION_MESSAGE);
		}
		return uz; 
	}

	/*
	 * Remove user from crowd 
	 */
	public void  removeUserFromCrowd(String uz ) throws UserNotFoundException, OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException{
		TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, uz); 
		SearchRestriction user = sterm;

		if (uzInfo.searchUsers(uz).size()!= 0) {
			userConn.removeUser(uz);
			//System.out.println("User has been removed");
		} else {
			JOptionPane.showMessageDialog(null, "User does not exist", "user to be removed not found", JOptionPane.INFORMATION_MESSAGE);
		}
	}


	/*
	 * remove user from a group 
	 */
	public void removeUserFromGroup(String uz, String grp ) throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException, MembershipNotFoundException, GroupNotFoundException{

		TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, uz); 
		SearchRestriction user = sterm;

		grp = grp + "(Team)";		// since team naming on crowd we added "(Team)" 
		TermRestriction grpSTerm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, grp); 
		SearchRestriction group = grpSTerm;

		if ( (userConn.searchGroups(grpSTerm, maxResults, startIndex).size()!=0) & (userConn.searchUsers(user, maxResults, startIndex).size() !=0)){
			userConn.removeGroupFromGroup(uz, grp);
			System.out.println("User removed from the group"); 
		} else {
			JOptionPane.showMessageDialog(null, "User/Group does not exist or you have enter incorrect infromation", "user to be removed not found", JOptionPane.INFORMATION_MESSAGE);

		}

	}

	/*
	 * add user to a group
	 */
	public void addUsertoGroup(String uz, String grp ) throws MembershipNotFoundException, GroupNotFoundException, HeadlessException, OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException, UserNotFoundException{
		//search to make sure the user and group exist before adding user to a group 

		TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, uz); 
		SearchRestriction user = sterm;

		grp = grp + " (Team)";		// since team naming on crowd we added "(Team)" 
		TermRestriction grpSTerm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, grp); 
		SearchRestriction group = grpSTerm;


		if ( checkIfUserExist(uz)==true && checkIfGroupExist(grp)==true){
			userConn.addUserToGroup(uz, grp);
			System.out.println("User removed from the group"); 
		} else {
			JOptionPane.showMessageDialog(null, "User/Group does not exist or you have enter incorrect infromation", "user to be removed not found", JOptionPane.INFORMATION_MESSAGE);

		}

	}

	//reset password request
	public void resetPassword(String uz) throws UserNotFoundException, HeadlessException, OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException, InvalidEmailAddressException{
		TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, uz); 
		SearchRestriction user = sterm;

		if (checkIfUserExist(uz) ==true){
			userConn.requestPasswordReset(uz);
			System.out.println("User removed from the group"); 
		} else {
			JOptionPane.showMessageDialog(null, "User does not exist", "User Not Found", JOptionPane.INFORMATION_MESSAGE);

		}


	}

	/*
	 * Check if user exist on the system
	 */
	public boolean checkIfUserExist(String uz) throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException{
		//TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, uz); 
		//SearchRestriction user = sterm;
		//uzInfo = new RetrieveUserInfo();

		if( (uzInfo.searchUsers(uz)).size()!=0){
			System.out.println("User EXISTYYYYYYYYYYYYYYYY");
			return true ; 
		} 
		else {
			return false;

		}
	}


	/*
	 * check to see if group exist on the system
	 */
	public boolean checkIfGroupExist(String grp) throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException{

		//TermRestriction sterm = new TermRestriction (UserTermKeys.USERNAME,MatchMode.EXACTLY_MATCHES, grp); 
		//SearchRestriction group = sterm;

		if(grpInfo.searchGroups(grp).size()!=0){
			return true; 
		}
		return false;

	}
}